#! /bin/bash

APP=gmall_s1
sqoop=/opt/module/sqoop/bin/sqoop

if [ -n "$2" ] ;then
   do_date=$2
else 
   echo "请传入日期参数"
   exit
fi 

import_data(){
$sqoop import \
--connect jdbc:mysql://hadoop102:3306/$APP \
--username root \
--password root \
--target-dir /origin_data/$APP/db/$1/$do_date \
--delete-target-dir \
--query "$2 where \$CONDITIONS" \
--num-mappers 1 \
--fields-terminated-by '\t' \
--compress \
--compression-codec lzop \
--null-string '\\N' \
--null-non-string '\\N'

hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/common/hadoop-lzo-0.4.20.jar com.hadoop.compression.lzo.DistributedLzoIndexer /origin_data/$APP/db/$1/$do_date
}

import_order_info(){
  import_data order_info "select
                            id, 
                            final_amount, 
                            order_status, 
                            user_id, 
                            create_time
                        from order_info"
}



import_user_info(){
  import_data "user_info" "select 
                            id,
                            name,
                            birthday,
                            create_time
                          from user_info"
}

import_order_detail(){
  import_data order_detail "select 
                              id,
                              order_id, 
                              sku_id,
                              order_price,
                              sku_num, 
                              split_final_amount,
                              create_time
                            from order_detail"
}

import_payment_info(){
  import_data "payment_info"  "select 
                                id,  
                                order_id, 
                                user_id, 
                                callback_time ,
                                create_time
                              from payment_info"
}

import_sku_info(){
  import_data sku_info "select 
                          id,
                          spu_id,
                          price,
                          sku_name,
                          category3_id,
                          create_time
                        from sku_info"
}

import_base_category1(){
  import_data "base_category1" "select 
                                  id,
                                  name 
                                from base_category1"
}

import_base_category2(){
  import_data "base_category2" "select
                                  id,
                                  name,
                                  category1_id 
                                from base_category2"
}

import_base_category3(){
  import_data "base_category3" "select
                                  id,
                                  name,
                                  category2_id
                                from base_category3"
}


import_spu_info(){
  import_data spu_info "select
                            id,
							spu_name,
							category3_id
                          from spu_info"
}

import_favor_info(){
  import_data favor_info "select
							sku_id,
							is_cancel,
							create_time
                        from favor_info"
}

import_cart_info(){
  import_data cart_info "select
						user_id,
						sku_id,
						create_time
                      from cart_info"
}
   

case $1 in
  "order_info")
     import_order_info
;;
  "base_category1")
     import_base_category1
;;
  "base_category2")
     import_base_category2
;;
  "base_category3")
     import_base_category3
;;
  "order_detail")
     import_order_detail
;;
  "sku_info")
     import_sku_info
;;
  "user_info")
     import_user_info
;;
  "payment_info")
     import_payment_info
;;
  "cart_info")
      import_cart_info
;;
  "favor_info")
      import_favor_info
;;
  "spu_info")
      import_spu_info
;;
  "all")
   import_base_category1
   import_base_category2
   import_base_category3
   import_order_info
   import_order_detail
   import_sku_info
   import_user_info
   import_payment_info
   import_cart_info
   import_favor_info
   import_spu_info
;;
esac
